import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'
import employees from './modules/employees'
import departments from './modules/departments'
import setting from './modules/setting'
import salarys from './modules/salarys'
import social from './modules/social'
import attendances from './modules/attendances'
import approvals from './modules/approvals'
import permissions from './modules/permissions'

// 动态路由(根据不同用户的权限进行变化 数量不是固定的)
export const asyncRoutes = [
  departments,
  setting,
  employees,
  permissions,
  approvals,
  attendances,
  salarys,
  social
]

// 静态路由(每个用户都可以正常访问，不会发生变化)
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  {
    path: '/uploadExcel',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      name: 'UploadExcel',
      component: () => import('@/views/uploadExcel/index')
    }]
  }

]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // 404 page must be placed at the end !!!
  // 404必须在最末尾
  // 动态路由表需要做权限控制 初始化时应只要静态路由表
  routes: [...constantRoutes] // 动态路由 + 静态路由
})

const router = createRouter()

// 重置路由方法
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  // 重新设置路由的可匹配路径
  router.matcher = newRouter.matcher // reset router
}

export default router
